<?php

session_start();
require_once '../kelas/record.php';

require_once '../kelas/kategori.php';
require_once '../kelas/produk.php';
require_once '../kelas/fungsi.php';
require_once '../kelas/amankanParam.php';
require_once '../kelas/pemesanan.php';
require_once '../kelas/kota.php';
$objR = new record();
$dtGet = $objParamAman->getMethod();
$dtPost = $objParamAman->postMethod();
$on = isset($dtGet['on']) ? $dtGet['on'] : '';
if (strlen($on) > 0) {
    switch ($on) {
        case 'finish':
        $dtPost['id_pemesanan'] = $objPemesanan->buatNoTrans();
        $dtPost['tanggal'] = date('Y-m-d H:i:s');
        $dtPost['idcus']=$_SESSION['userlogin']['id'];
        $dtPost['biaya_ongkir']=$objKota->getBiaya($dtPost['ongkir']);
        $dtPost['status_pesan'] = 'TUNGGU_PEMBAYARAN';
        $objPemesanan->create($dtPost);
        $ex = $objPemesanan->execute();
        if ($ex['success'] == true){
            $arrKey = 0;
                foreach ($_SESSION['detail_cart'] as $k=> $psnDet) {//atur ulang data yg akan di simpan
                    $dtSvDetail[$arrKey]['id_produk']=$psnDet['id'];
                    $dtSvDetail[$arrKey]['jumlah_buy']=$psnDet['jumlah_buy'];
                    $dtSvDetail[$arrKey]['harga']=$psnDet['harga_jual'];
                    $dtSvDetail[$arrKey]['h_modal']=$psnDet['harga_modal'];
                    $dtSvDetail[$arrKey]['diskon']=$psnDet['diskon'];
                    $dtSvDetail[$arrKey]['id_pesanan']=$dtPost['id_pemesanan'];
                    $dtSvDetail[$arrKey]['subtotal']=$psnDet['harga_jual']*$psnDet['jumlah_buy'];                    
                    $arrKey++;
                };
                foreach ($dtSvDetail as $v){
                    $objPemesanan->addDetail($v);
                    $al= $objPemesanan->execute();
                    if($al['success']==false){
                        $ex=$al;
                    }
                }
            }
            $rer = '?v=ordersuccess&ord='.$dtPost['id_pemesanan'];  
            if ($ex['success'] == false) {
                echo $objFungsi->alert($ex['error_msg'].":".$ex['error_query']);
                $rer = '?v=checkout';
            }
            else{
                $dataSendMail = $objPemesanan->getDataPesan($dtPost['id_pemesanan']);
                $objPemesanan->sendOrderMail($dtPost['id_pemesanan'],$_SESSION['userlogin']['email']);
                unset($_SESSION['detail_cart']);
            };
            echo $objFungsi->redirect(BASEURL . $rer);
            break;
            case 'proses':
            $dtPost['idadmin']=$_SESSION['adminlogin']['id'];
            $dtW['id_pemesanan'] = $dtPost['id_pemesanan'];
            $dtW['id']=$dtPost['id'];
            unset($dtPost['id']);
            unset($dtPost['id_pemesanan']);
            $objPemesanan->update($dtPost,$dtW);
            $u  = $objPemesanan->execute();
            echo json_encode($u);
            break;
            default:
            echo json_encode(array('success'=>false,'error_msg'=>'Position Not Set Yet'));
            break;
        }
    } else {
        echo $objFungsi->alert('Akses Langsung Ditolak');
        echo $objFungsi->redirect(BASEURL);
    }